package chapter13;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/20 16:43
 */
class Student {
    private String name;
    private int age;
    private double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}

public class StreamTest {
    public static void main(String[] args) {
        String filePath = "D:\\hello.txt";
        testDataOutputSteam(filePath);
    }

    private static byte[] readFileByTransfer(String fileName) {
        try (InputStream input = new FileInputStream(fileName);
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
//            input.transferTo(outputStream);
            return outputStream.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new byte[0];
    }

    private static void testDataOutputSteam(String filePath) {
        List<Student> students = Arrays.asList(new Student[]{
                new Student("张三", 18, 89.87),
                new Student("李四", 17, 90.1)
        });

        try (DataOutputStream output = new DataOutputStream(new FileOutputStream(filePath, true))) {
            output.writeInt(students.size());
            output.writeUTF("\n\r");
            for (Student student : students) {
                output.writeUTF(student.getName());
                output.writeInt(student.getAge());
                output.writeDouble(student.getScore());
                output.writeUTF("\n\r");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void testReadByByteArrayOutputSteam(String filePath) {
        try (InputStream input = new BufferedInputStream(new FileInputStream(filePath));
             ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[1024];
            int byteRead = 0;
            while ((byteRead = input.read(buffer)) != -1) {
                output.write(buffer, 0, byteRead);
            }
            String data = output.toString("UTF-8");
            System.out.println(data);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testWrite(String filePath) {
        try (OutputStream output = new FileOutputStream(filePath, true)) {
            String data = "hello,123,老马\n\r";
            byte[] bytes = data.getBytes(Charset.forName("utf-8".toUpperCase()));
            output.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testReadByStream(String filePath) {
        try (InputStream input = new FileInputStream(filePath)) {
            byte[] buf = new byte[1024];
            int byteRead;
            StringBuilder builder = new StringBuilder();
            while ((byteRead = input.read(buf)) != -1) {
                String data = new String(buf, 0, byteRead, "UTF-8");
                builder.append(data);
            }

            System.out.println(builder.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
